package com.globallogic.vacationplanner.model.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.globallogic.vacationplanner.model.entity.Project;
import com.globallogic.vacationplanner.model.entity.User;
import com.globallogic.vacationplanner.transfer.dto.impl.ProjectDto;
import com.globallogic.vacationplanner.transfer.dto.impl.UserDto;

public class ProjectDtoHelper {
	private static Logger LOGGER = Logger.getLogger(ProjectDtoHelper.class);

	public static List<ProjectDto> getProjectDtoList(List<Project> projectsList) {
		LOGGER.info("Getting all projects ");
		List<ProjectDto> projectDtoList = new ArrayList<ProjectDto>();
		for (Project project : projectsList) {
			ProjectDto projectDto = convertProjectToProjectDto(project);
			projectDtoList.add(projectDto);
		}
		LOGGER.info("Getting success");
		return projectDtoList;
	}

	public static List<ProjectDto> getUserProjectDtoList(
			Set<Project> projectsList) {
		LOGGER.info("Getting user projects");
		List<ProjectDto> projectDtoList = new ArrayList<ProjectDto>();
		for (Project project : projectsList) {
			ProjectDto projectDto = convertProjectToProjectDto(project);
			projectDtoList.add(projectDto);
		}
		LOGGER.info("Getting success");
		return projectDtoList;
	}

	public static UserDto convertUserToUserDto(User dbUser) {
		LOGGER.info("Convert User to UserDto");
		if (dbUser != null) {
			UserDto userDto = new UserDto();
			userDto.setLogin(dbUser.getLogin());
			userDto.setUserName(dbUser.getUsername());
			userDto.setSurname(dbUser.getSurname());
			userDto.setEmail(dbUser.getEmail());
			LOGGER.info("Convert success");
			return userDto;
		}else {
			return null;
		}
		
	}

	public static ProjectDto convertProjectToProjectDto(Project project) {
		LOGGER.info("Convert Project to ProjectDto");
		ProjectDto projectDto = new ProjectDto();
		projectDto.setIdProjects(project.getIdProject());
		projectDto.setLoginCreator(project.getUser().getLogin());
		projectDto.setName(project.getName());
		projectDto.setDescription(project.getDescription());
		projectDto.setState(project.getState());
		projectDto.setUserMembersList(convertUserSetToUserDtoList(project
				.getMemberedUsers()));
		LOGGER.info("Convert success");
		return projectDto;
	}

	/**
	 * Converting set User members of project to list UserDto members of project
	 * 
	 * @param members
	 * @return list
	 */
	public static List<UserDto> convertUserSetToUserDtoList(Set<User> members) {
		LOGGER.info("Convert success");
		List<UserDto> projectDtoMembers = new ArrayList<UserDto>();
		for (User member : members) {
			projectDtoMembers.add(convertUserToUserDto(member));
		}
		LOGGER.info("Convert success");
		return projectDtoMembers;
	}
	//
	// public static Map<ProjectDto, String> getUserProjectWithKeys(
	// Set<Project> memberedProject, Set<Project> ownProjects) {
	// Map<ProjectDto, String> userMap = new HashMap<ProjectDto, String>();
	// for (Project ownProject : ownProjects) {
	// userMap.put(convertProjectToProjectDto(ownProject),"Creator");
	// }
	// for (Project memberProject : memberedProject) {
	// userMap.put(convertProjectToProjectDto(memberProject),"Member");
	// }
	// return userMap;
	// }

}
